/**  
 * @Title: ActorAdmin.java
 * @Package com.limit.control.admin
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-3-30 上午09:42:13
 * @version V1.0
 */ 
package com.limit.control.admin;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.ehcache.CacheKit;
import com.limit.constant.Constant;
import com.limit.control.admin.validate.ActorValidator;
import com.limit.model.Actor;
import com.limit.model.ActorModule;
import com.limit.model.Module;

/** 
 * <br/>
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-3-30 上午09:42:13 
 * @version V1.0
 */
public class ActorController extends BaseController{

	/**
	 * 角色列表
	 */
	public void index(){
		List<Actor> actors = Actor.dao.find("select * from actor");
		this.setAttr("actors", actors);
		render("actors.html");
	}
	
	/**
	 * 跳转视图
	 */
	public void view(){
		this._commonSetId();
		Actor actor = new Actor();
		if(id > 0){
			actor = Actor.dao.findById(id);
			// 获取actor_id对应的权限列表
			String old_modules = new String(); 
			List<ActorModule> moduleList = actor.getAdminModuleList();
			if(null != moduleList && !moduleList.isEmpty()){
				for (ActorModule actorModule : moduleList) {
					Module module = actorModule.getModule();
					BigInteger pid = module.get("pid");
					if(pid.longValue()==0){
						old_modules = old_modules + module.get("id") + ","; 
					}
				}
			}
			this.setAttr("old_modules", old_modules);
		}
		this.setAttr("modules", Module.dao.getListByPid(0));
		this.setAttr("actor", actor);
		render("actor_view.html");
		
	}

	/**
	 * 保存
	 */
	@Before(ActorValidator.class)
	public void save(){
		Actor actor = this.getModel(Actor.class);
		String[] parentModules = this.getParaValues("modules");
		List<Module> list = new ArrayList<Module>();
		if(parentModules.length > 0){
			for (String id : parentModules) {
				// pids
				List<Module> children = Module.dao.getListByPid(Integer.parseInt(id));
				children.add(Module.dao.set("id", id).set("pid", 0));
				list.addAll(children);
			}
		}
		actor.save();
		long id = actor.get("id");
		if(!list.isEmpty()){
			List<String> sqlList = new ArrayList<String>();
			String sql = new String();
			for (Module module : list) {
				sql = "INSERT INTO actor_module(`actor_id`,`module_id`) VALUES (" + id + ","+module.get("id")+")";
				sqlList.add(sql);
			}
			ActorModule.dao.batchOperate(sqlList);
		}
		// 将actor和module关联起来
		this.index();
		
	}
	
	/**
	 * 更新
	 * 
	 */
	public void update(){
		Actor actor = this.getModel(Actor.class);
		// 提交的权限数组
		String[] modules = this.getParaValues("modules");
		// 原来的权限数组
		String old_module = this.getPara("old_modules");
		if(old_module.length()>0){
			old_module = old_module.substring(0, old_module.length()-1);
		}
		String[] old_modules = old_module.split(",");
		// 比较哪个是新增的,哪个是删减的并分别保存下来
		List<String> tempNew = new ArrayList<String>();
		if(null!=modules&&modules.length>0){
			tempNew = Arrays.asList(modules);	
		}
		List<String> tempOld = Arrays.asList(old_modules);
		List<String> add = new ArrayList<String>();
		List<String> del = new ArrayList<String>();
		for (String string : tempNew) {
			if(!tempOld.contains(string)&&!string.equals("")){
				add.add(string);
			} 
		}
		for (String string : tempOld) {
			if(!tempNew.contains(string)&&!string.equals("")){
				del.add(string);
			}
		}
		// 分别找出新增/删减数组里模块所有关联的数据。最后save或delete
		List<Module> addAll = new ArrayList<Module>();	//所有save的列表 
		for (String string : add) {
			System.out.println(string);
			List<Module> addTemp = Module.dao.getListByPid(Long.parseLong(string));
			addTemp.add(Module.dao.findById(Long.parseLong(string)));
			addAll.addAll(addTemp);
		}
		List<Module> delAll = new ArrayList<Module>();	//所有delete的列表
		for (String string : del) {
			List<Module> delTemp = Module.dao.getListByPid(Long.parseLong(string));
			delTemp.add(Module.dao.findById(Long.parseLong(string)));
			delAll.addAll(delTemp);
		}
		//更新权限缓存
		actor.update();
		long id = actor.get("id");
		//save
		List<String> sqlList = new ArrayList<String>();
		String sql = new String();
		for (Module module : addAll) {
			sql = "INSERT INTO actor_module(`actor_id`,`module_id`) VALUES (" + id + "," + module.get("id") + ");";
			sqlList.add(sql);
		}
		if(!sqlList.isEmpty()){
			ActorModule.dao.batchOperate(sqlList);
		}
		//delete
		sqlList.removeAll(sqlList);
		for (Module module : delAll) {
			sql = "DELETE FROM actor_module WHERE actor_id = " + id + " and module_id = " + module.get("id") + ";";
			sqlList.add(sql);
		}
		if(!sqlList.isEmpty()){
			ActorModule.dao.batchOperate(sqlList);
		}
		String actorId = String.valueOf(this.getSessionAttr(Constant.ActorId));
		String username = String.valueOf(this.getSessionAttr(Constant.UserName));
		this._refreshPermissionCache( actorId , username);
		this.index();
		
	}
	
	/**
	 * 删除操作
	 */
	public void delete(){
		this._commonSetId();
		this.getModel(Actor.class).deleteById(id);
		this.index();
	}
	
	
	
	
}
